<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <el-form-item label="报表名称" prop="name">
        <ns-input v-model="formData.name" placeholder="请输入报表名称" />
      </el-form-item>
      <el-form-item label="报表模板" prop="tempId">
        <el-select v-model="formData.tempId" clearable placeholder="请选择报表模板">
          <el-option
            @click="changeTemp(item)"
            v-for="(item, index) in tempList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="默认期间" prop="defaultTime">
        <el-select v-model="formData.defaultTime" clearable placeholder="请选择默认期间">
          <el-option label="全部" :value="0" />
          <el-option label="本日" :value="1" />
          <el-option label="本周" :value="2" />
          <el-option label="本月" :value="3" />
          <el-option label="本年" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <ns-input v-model="formData.remark" clearable placeholder="请输入备注" />
      </el-form-item>
      <!-- <el-form-item label="数据起始时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          type="date"
          placeholder="年/月/日"
          format="YYYY/MM/DD"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="数据截至时间" prop="endTime">
        <el-date-picker
          v-model="formData.endTime"
          type="date"
          placeholder="年/月/日"
          format="YYYY/MM/DD"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submitForm">下一步</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import * as reportTempApi from '@/api/report/reportTemp/index'
import * as reportManageApi from '@/api/report/reportManage/index'
defineOptions({ name: 'StandardsForm' })

const router = useRouter()
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改

const formData = ref({
  id: undefined,
  name: undefined,
  tempId: undefined,
  showHomePage: 0,
  temp: undefined,
  startTime: undefined,
  endTime: undefined,
  defaultTime: undefined,
  remark: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '报表名称不能为空', trigger: 'blur' }],
  tempId: [{ required: true, message: '报表模板不能为空', trigger: 'change' }],
  startTime: [{ required: true, message: '数据起始时间不能为空', trigger: 'change' }],
  endTime: [{ required: true, message: '数据截止时间不能为空', trigger: 'change' }],
  defaultTime: [{ required: true, message: '默认周期不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, form?) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await reportManageApi.reportManageGet({ id: id })
    } finally {
      formLoading.value = false
    }
  }
  if (form) {
    formData.value = { ...form, defaultTime: Number(form.defaultTime) }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const changeTemp = (e) => {
  console.log('e', e)
  formData.value.temp = e.name
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = {
      status: 0,
      ...formData.value
    }
    router.push({
      path: '/report/reportEdit2',
      query: data
    })
    dialogVisible.value = false
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    tempId: undefined,
    showHomePage: 0,
    temp: undefined,
    startTime: undefined,
    endTime: undefined,
    defaultTime: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}

const tempList = ref()
const getTempList = async () => {
  const { data } = await reportTempApi.reportTempPage({
    page: 1,
    pageSize: 9999
  })
  tempList.value = data
  console.log('data', data)
}
onMounted(() => {
  getTempList()
})
</script>
